﻿Imports System.Runtime.Serialization
Imports System.ComponentModel
Imports ATS.Business.Entities.EntityStateClass
Imports System.Collections.ObjectModel

<DataContract()> _
<Serializable()> _
Partial Public Class CompanyDetail
    Implements INotifyPropertyChanged

    Private _isDirty As Boolean
#Region "Primitive Properties"

    Private _CompanyName As String
    <DataMember()>
    Public Property CompanyName() As String
        Get
            Return _CompanyName
        End Get

        Set(ByVal value As String)
            If Not value Is Nothing Then
                If value <> _CompanyName Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "CompanyName")
                End If
            Else
                Throw New Exception("Please provide value for Company name.")
            End If
            _CompanyName = value
            OnPropertyChanged("CompanyName")
        End Set
    End Property

    <DataMember()>
    Public Overridable Property CompanyID As Integer
   
    Public ReadOnly Property IsDirty() As Boolean
        Get
            Return Me.EntityState <> EntityStateEnum.Unchanged
        End Get
    End Property
#End Region

#Region "Support Procedures"
    Public Sub ResetDirtyBit()
        Me.EntityState = EntityStateEnum.Unchanged
    End Sub

    Public Sub OnPropertyChanged(ByVal propertyName As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub

    Public Event PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

    Private _EntityState As EntityStateEnum
    Public Property EntityState() As EntityStateEnum
        Get
            Return _EntityState
        End Get
        Private Set(ByVal value As EntityStateEnum)
            _EntityState = value
        End Set
    End Property

    Private Sub DataStateChanged(ByVal dataState As EntityStateEnum, ByVal propertyName As String)
        ' Raise the event
        If Not String.IsNullOrEmpty(propertyName) Then
            RaiseEvent PropertyChanged(Me, _
            New PropertyChangedEventArgs(propertyName))
        End If

        ' If the state is deleted, mark it as deleted
        If dataState = EntityStateEnum.Deleted Then
            Me.EntityState = dataState
        End If

        If Me.EntityState = _
                       EntityStateEnum.Unchanged Then
            Me.EntityState = dataState
        End If
    End Sub
#End Region
End Class